package com.example.hotel_lxy.ui.activity;

import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.example.hotel_lxy.R;
import com.example.hotel_lxy.application.ARouterPath;
import com.example.hotel_lxy.bean.Bean;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.OfficialWeChatBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.utils.SPUtils;
import com.ysxsoft.common_base.base.BaseActivity;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 官方微信
 */
@Route(path = "/main/OfficialWeChatActivity")
public class OfficialWeChatActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.text_1)
    TextView text1;
    @BindView(R.id.text_2)
    TextView text2;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.img_wechat)
    ImageView imgWechat;

    public static void start() {
        ARouter.getInstance().build(ARouterPath.getOfficialWeChatActivity())
                .navigation();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_official;
    }

    @Override
    public void doWork() {
        super.doWork();
        back.setOnClickListener(this);

        text2.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        text2.getPaint().setAntiAlias(true);//抗锯齿
        text2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showintent();
            }
        });
        text1.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                showintent();
                return true;
            }
        });
        showData();
    }

    private void showData() {
        String token = SPUtils.get(OfficialWeChatActivity.this, "token", "").toString();
        showLoadingDialog("请求中");
        OkHttpUtils.post()
                .url(Api.POST_SET)
                .addHeader("flag", token)
                .addParams("type", "2")
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "官方微信Exception~~~~~~~~    " + e.getMessage());
                        hideLoadingDialog();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "官方微信onResponse~~~~~~~~    " + response);
                        hideLoadingDialog();
                        BeanBean beanBean=JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("0")) {
                            OfficialWeChatBean officialWeChatBean = JsonUtils.parseByGson(response, OfficialWeChatBean.class);
                            Glide.with(OfficialWeChatActivity.this).load(officialWeChatBean.getData().getList().getTitle()).into(imgWechat);
                        }else if (beanBean.getCode().equals("4")) {
                            SPUtils.put(OfficialWeChatActivity.this,"token","");
                            Intent intent = new Intent(OfficialWeChatActivity.this, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }
                    }
                });
    }

    private void showintent() {
        try {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(cmp);
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            // TODO: handle exception
            Toast.makeText(this, "检查到您手机没有安装微信，请安装后使用该功能", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}